package com.ywjh.inter.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.ywjh.inter.entity.EquInsulinStatEntity;

public class EquInsulinStatDAO {
	public boolean insert(Connection conn, EquInsulinStatEntity entity) throws SQLException {
		PreparedStatement ps = conn.prepareStatement(
				"insert into equ_insulin_stat(equ_no,equ_time,power_less,dosage_less,add_time) values(?,?,?,?,?)");
		boolean b = false;
		ps.setString(1, entity.getEqu_no());
		ps.setTimestamp(2, entity.getEqu_time());
		ps.setFloat(3, entity.getPower_less());
		ps.setFloat(4, entity.getDosage_less());
		ps.setTimestamp(5, entity.getAdd_time());
		if (ps.executeUpdate() > 0) {
			b = true;
		}
		return b;
	}

	public boolean update(Connection conn, EquInsulinStatEntity entity) throws SQLException {
		PreparedStatement ps = conn.prepareStatement(
				"update equ_insulin_stat set equ_no=?,equ_time=?,power_less=?,dosage_less=?,add_time=? where id=?");
		boolean b = false;
		ps.setString(1, entity.getEqu_no());
		ps.setTimestamp(2, entity.getEqu_time());
		ps.setFloat(3, entity.getPower_less());
		ps.setFloat(4, entity.getDosage_less());
		ps.setTimestamp(5, entity.getAdd_time());
		ps.setInt(6, entity.getId());
		if (ps.executeUpdate() > 0) {
			b = true;
		}
		return b;
	}

	public EquInsulinStatEntity findByEquNo(Connection conn, String equ_no) throws SQLException {
		EquInsulinStatEntity e = null;
		PreparedStatement ps = conn.prepareStatement("select * from equ_insulin_stat where equ_no =?");
		ps.setString(1, equ_no);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			e = fill(rs);
		}
		return e;
	}

	private EquInsulinStatEntity fill(ResultSet rs) throws SQLException {
		EquInsulinStatEntity e = new EquInsulinStatEntity();
		e.setId(rs.getInt("id"));
		e.setAdd_time(rs.getTimestamp("add_time"));
		e.setDosage_less(rs.getFloat("dosage_less"));
		e.setEqu_no(rs.getString("equ_no"));
		e.setEqu_time(rs.getTimestamp("equ_time"));
		e.setPower_less(rs.getFloat("power_less"));
		return e;
	}

}
